﻿using System;
using System.Reflection;
using NUnit.Core;
using SeleniumToolkit.Core;

namespace SeleniumToolkit.NUnit
{
    /// <summary>
    /// TestMethods marked with [WebTest]
    /// </summary>
    internal class WebTestCase : NUnitTestMethod, ISessionInfo
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="method"></param>
        public WebTestCase(MethodInfo method) : base(method) 
        {
        }

#if (NUNIT2_5_3 || NUNIT2_5_5 || NUNIT2_5_7)
        /// <summary>
        /// Runs the WebTest
        /// </summary>
        /// <param name="testResult"></param>
        public override void Run(TestResult testResult)
        {
            try
            {
                using (new Browser(this))
                {
                    base.Run(testResult);
                }
            }
            catch (Exception ex)
            {
                testResult.Error(ex, FailureSite.Test);
            }
        }
#endif

#if (NUNIT2_5_8 || NUNIT2_5_9)
        /// <summary>
        /// Runs the Web Test
        /// </summary>
        /// <returns></returns>
        public override TestResult RunTest()
        {
            TestResult result;
            using (new Browser(this))
            {
                result = base.RunTest();
            }

            return result;
        }
#endif

        #region ISessionInfo Members

        /// <summary>
        /// Base Url for the WebTest
        /// </summary>
        public string BaseUrl
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set a flag to indicate whether this test owns the selenium session
        /// </summary>
        /// <remarks>
        /// When set to false, the fixture owns the session
        /// </remarks>
        public bool SessionOwner
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set the Browser type for the test
        /// </summary>
        public string DefaultBrowser
        {
            get;
            set;
        }

#pragma warning disable 108,114
        /// <summary>
        /// Gets or sets the Selenium Timeout value (not the same as TestCase Timeout)
        /// </summary>
        public string Timeout // TODO: Selenium Timeout isn't the same as TestCase timeout, should fix.
#pragma warning restore 108,114
        {
            get; set; 
        }

        #endregion
    }
}
